﻿using System;
using System.Collections.Generic;
using System.Text;
using Wangzeran.CodeMaker.GUI.Utils;

namespace Wangzeran.CodeMaker.GUI.Modal
{
    /// <summary>
    /// 数据库连接信息
    /// </summary>
    public class DBModal
    {
        /// <summary>
        /// MS SQL Server数据库
        /// </summary>
        public const string MSSQL = "mssql";
        /// <summary>
        /// MySql数据库
        /// </summary>
        public const string MYSQL = "mysql";
        /// <summary>
        /// Access数据库
        /// </summary>
        public const string ACCESS = "access";

        /// <summary>
        /// 默认的构造方法
        /// </summary>
        public DBModal()
        {

        }

        /// <summary>
        /// 解析连接字符串
        /// </summary>
        /// <param name="connString">连接字符串</param>
        public DBModal(string type, string connString)
        {
            try
            {
                IDictionary<string, string> setting = StringHelper.ParseKeyValues(connString);
                this.databaseType = type;
                switch (type)
                {
                    case "mssql2000":
                    case "mssql2005":
                        {
                            ParseMSSQLConnString(setting);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            catch (Exception ex)
            {	
                throw ex;
            }

        }

        /// <summary>
        /// 解析MSSQL连接字符串
        /// </summary>
        /// <param name="setting"></param>
        private void ParseMSSQLConnString(IDictionary<string, string> setting)
        {
            try
            {
                if (setting.ContainsKey("data source"))
                {
                    this.dataSource = setting["data source"];
                }
                if (setting.ContainsKey("databasename"))
                {
                    this.databaseName = setting["databasename"];
                }
                if (setting.ContainsKey("integrated security"))
                {
                    string authzMode = setting["integrated security"].ToLower();
                    this.isWindowsAuthz = (authzMode == "sspi" || authzMode == "true");
                }
                if (setting.ContainsKey("uid"))
                {
                    this.user = setting["uid"];
                }
                if (setting.ContainsKey("pwd"))
                {
                    this.pwd = setting["pwd"];
                }
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }


        #region Fields

        /// <summary>
        /// 数据库类型
        /// </summary>
        private string databaseType;
        /// <summary>
        /// 服务器地址
        /// </summary>
        private string dataSource;
        /// <summary>
        /// 数据库名称
        /// </summary>
        private string databaseName;
        /// <summary>
        /// 是否为Windows验证方式
        /// </summary>
        private bool isWindowsAuthz;
        /// <summary>
        /// 用户名
        /// </summary>
        private string user;
        /// <summary>
        /// 密码
        /// </summary>
        private string pwd;

        #endregion


        #region Properties

        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DatabaseType
        {
            get { return databaseType; }
            set { databaseType = value; }
        }
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }
        /// <summary>
        /// 数据库名称
        /// </summary>
        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }
        /// <summary>
        /// 是否为Windows验证方式
        /// </summary>
        public bool IsWindowsAuthz
        {
            get { return isWindowsAuthz; }
            set { isWindowsAuthz = value; }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public string User
        {
            get { return user; }
            set { user = value; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }

        #endregion

        /// <summary>
        /// 返回相应的连接字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            try
            {
                string connString = null;
                switch (databaseType)
                {
                    case "mssql2000":
                    case "mssql2005":
                        {
                            if (this.isWindowsAuthz)
                            {
                                connString = String.Format("Data Source={0};Integrated Security=SSPI;",
                                    this.dataSource);
                            }
                            else
                            {
                                connString = String.Format("Data Source={0};Uid={1};Pwd={2};",
                                    this.dataSource, this.user, this.pwd);
                            }

                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                return connString;
            }
            catch (Exception ex)
            {	
                throw ex;
            }
        }

    }
}
